﻿using Microsoft.Win32;
using PMSystem.ApplicationLogics;
using PMSystem.DataAccess;
using PMSystem.DataAccess.DataRepository;
using PMSytem.Windows.App.Pages;
using PMSytem.Windows.App.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PMSytem.Windows.Tests
{

    public partial class AddUser : ToolWindow
    {

        UserService _service;
        private int currentUserid = 0;



        public AddUser()
        {
            InitializeComponent();
            _service = new UserService();
            currentUserid = 0;
            DataContext = this;
        }

        public AddUser(int userid)
        {
            InitializeComponent();
            _service = new UserService();
            currentUserid = userid;
            DataContext = this;
            LoadEditUser();
        }

        private void LoadEditUser()
        {
            var user = _service.GetUserByUserId(currentUserid);
            if(user!=null)
            {
                txtfname.Text = user.FirstName;
                txtlname.Text = user.LastName;
                txtusername.Text = user.UserName;
                txtusername.IsEnabled = false;
            }

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var status = 0;
                if (currentUserid == 0)
                {
                    var isUsernameExists = _service.IsUserNameAvailable(txtusername.Text);
                    if(isUsernameExists)
                    {

                        MessageBox.Show("User Name already exists.", "Add User Status",
                                MessageBoxButton.OK,
                                MessageBoxImage.Warning,
                                MessageBoxResult.OK,
                                null);
                        return;
                    }
                    var user = new User()
                    {
                        FirstName = txtfname.Text,
                        LastName = txtlname.Text,
                        UserName = txtusername.Text.Trim(),
                        Password = txtpassword.Password.Trim(),
                        IsDeleted = false
                    };
                    status = _service.CreateOrUpdateUser(user);
                    if (status > 0)
                    {
                        AuditManager.AddAuditRecord(AuditEntity.CreateUser, "create a new user( user name:" + user.UserName + ")"); 
                        AddUserToRoles(user.UserId);

                        MessageBox.Show("Successfully added.", "Add User Status",
                                MessageBoxButton.OK,
                                MessageBoxImage.Information,
                                MessageBoxResult.OK,
                                null);

                        ClearFields();
                    }
                }
                else if(currentUserid>0)
                {
                     var user = _service.GetUserByUserId(currentUserid);
                     if (user != null)
                     { 
                      user.FirstName = txtfname.Text;
                      user.LastName = txtlname.Text;
                      user.UserName = txtusername.Text.Trim();
                      if (!string.IsNullOrWhiteSpace(txtpassword.Password))
                      user.Password = txtpassword.Password.Trim();
                     }
                     status = _service.CreateOrUpdateUser(user);
                     if (status > 0)
                     {
                         AuditManager.AddAuditRecord(AuditEntity.EditUser, "update a user( user name:" + user.UserName + ")"); 
                         AddUserToRoles(user.UserId);

                         MessageBox.Show("Successfully updated.", "Add User Status",
                                 MessageBoxButton.OK,
                                 MessageBoxImage.Information,
                                 MessageBoxResult.OK,
                                 null);

                         ClearFields();
                     }
                }
                   
                
                    
            }
            catch (Exception ex)
            {
                var errr = ex.Message;
            }
        }

        private bool IsPasswordMatch()
        {
            if (txtpassword.Password == txtconfrmpassword.Password)
                return true;
            return false;
        }


        private void AddUserToRoles(int userid)
        {
            List<string> roles = new List<string>();
            if ((bool)chkAdmin.IsChecked)
                roles.Add(chkAdmin.Content.ToString());
            if ((bool)chkOperator.IsChecked)
                roles.Add(chkOperator.Content.ToString());
            if ((bool)chkUser.IsChecked)
                roles.Add(chkUser.Content.ToString());

            _service.AddUserToRoles(userid, roles);
        }


        private void ClearFields()
        {
            txtfname.Text = txtlname.Text = txtusername.Text = txtpassword.Password = txtconfrmpassword.Password = "";
            chkAdmin.IsChecked = chkOperator.IsChecked = chkUser.IsChecked = false;
        }
    }
}